/*
 * @lc app=leetcode.cn id=128 lang=java
 *
 * [128] 最长连续序列
 */

// @lc code=start

import java.util.HashSet;
import java.util.Set;

class Solution {
    public int longestConsecutive(int[] nums) {
        Set<Integer> set = new HashSet<>();
        int ans = 0;
        for(int num : nums){
            set.add(num);
        }
        for(int num : set){
            if(!set.contains(num - 1)){
                int temp = 1;
                while(set.contains(++num)) {
                    temp++;
                }
                ans = max(temp, ans);
            }
        }
        return ans;
    }
    private static int max(int a, int b){
        if (a>b) {
            return a;
        }
        return b;
    }
}
// @lc code=end

